//
//  RHNetworkTagVC.m
//  RuiHeAppApp
//
//  Created by Yan Chen on 2023/11/20.
//

#import "RHNetworkTagVC.h"
#import "RHTagCell.h"
#import "RHTagModel.h"

@interface RHNetworkTagVC ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic ,strong) UITableView *tableView;

@property (nonatomic ,copy) NSArray *dataArr;

@property (nonatomic, copy) NSString *titleString;

@property (nonatomic, assign) NSInteger selectIdx;
@end

@implementation RHNetworkTagVC


- (instancetype)initWithData:(NSArray *)dataArray{
    
    self = [super init];
    if (self) {
        self.dataArr = dataArray;
    }
    return self;
}

- (instancetype)initWithTitle:(NSString*)title Data:(NSArray *)dataArray
{
    self = [super init];
    if (self) {
        self.dataArr = dataArray;
        self.titleString = title;

    }
    return self;
}

- (NSArray *)dataArr{
    if (!_dataArr) {
        _dataArr = [NSArray array];
    }
    return _dataArr;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.selectIdx = -1;
    [self setupUI];
}

- (void)setupUI{
    kWeakSelf;
    [self.view setBackgroundColor:[UIColor.blackColor colorWithAlphaComponent:0.3]];
    
    UIView *touchView = [[UIView alloc] init];
    [touchView setBackgroundColor:UIColor.clearColor];
    [self.view addSubview:touchView];

    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] init];
    touchView.userInteractionEnabled = YES;
    [touchView addGestureRecognizer:tap];
    
    UIView *containView = kView;
    containView.userInteractionEnabled = YES;
    containView.backgroundColor = [UIColor whiteColor];
    containView.layer.cornerRadius = 15;
    containView.layer.masksToBounds = YES;
    [self.view addSubview:containView];
    [containView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(30);
        make.right.mas_offset(-30);
        make.centerY.mas_offset(0);
   
    }];
    
    
    
    UIImageView *top_bgImg = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"network_tag_bg"]];
    [containView addSubview:top_bgImg];
    [top_bgImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.mas_offset(0);
        make.height.equalTo(top_bgImg.mas_width).multipliedBy(0.5);
    }];
    
   
    
    QMUILabel *titleLab = [[QMUILabel alloc]init];
    titleLab.textColor = kCOLOR_HEX(@"#171A1D");
    titleLab.font = kBOLDSYSTEMFONT(20);
    titleLab.text = @"请选择离线原因".rh_localized;
    [containView addSubview:titleLab];
    
    [titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_offset(35);
        make.centerX.mas_offset(0);
    }];
    
  
    
    
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, kUI_WINDOW_H-(216+kTabbarSafeBottomMargin), kUI_WINDOW_W, 216+kTabbarSafeBottomMargin)];
    [_tableView registerClass:[RHTagCell class] forCellReuseIdentifier:@"RHTagCell"];
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [_tableView setDataSource:self];
    [_tableView setDelegate:self];
    [_tableView setBackgroundColor:UIColor.whiteColor];
    [containView addSubview:_tableView];
    
    [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_offset(0);
        make.top.equalTo(titleLab.mas_bottom).offset(15);
        make.height.offset(weakSelf.dataArr.count * 60);
    }];
    
//    UIImageView *btn_img = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"network_tag_sure"]];
//    btn_img.contentMode = UIViewContentModeScaleAspectFill;
//    [containView addSubview:btn_img];
    
    UIButton *sureBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [sureBtn setTitleColor:kCOLOR_HEX(@"#ffffff") forState:UIControlStateNormal];
    [sureBtn setTitle:@"确定".rh_localized forState:UIControlStateNormal];
    [sureBtn.titleLabel setFont:[UIFont boldSystemFontOfSize:16]];
    sureBtn.backgroundColor = kCOLOR_HEX(@"#405DBE");
    sureBtn.layer.cornerRadius = 8;
    sureBtn.layer.masksToBounds = YES;
    [containView addSubview:sureBtn];
    
//    [btn_img mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.right.top.bottom.equalTo(sureBtn);
//    }];

    QMUIButton *cancelBtn = [QMUIButton buttonWithType:UIButtonTypeCustom];
    [cancelBtn setTitleColor:kCOLOR_HEX(@"#CCCCCC") forState:UIControlStateNormal];
    cancelBtn.layer.cornerRadius = 8;
    cancelBtn.layer.borderWidth = 1;
    cancelBtn.layer.borderColor = kCOLOR_HEX(@"CCCCCC").CGColor;
    cancelBtn.layer.masksToBounds = YES;
    [cancelBtn setTitle:@"取消".rh_localized forState:UIControlStateNormal];
    [cancelBtn.titleLabel setFont:[UIFont boldSystemFontOfSize:16]];
    [containView addSubview:cancelBtn];
    
    [sureBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.tableView.mas_bottom).offset(15);
        make.height.mas_equalTo(40);
        make.right.mas_offset(-18);
        make.bottom.mas_offset(-18);
    }];
    
    [cancelBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(18);
        make.bottom.mas_offset(-18);
        make.right.equalTo(sureBtn.mas_left).offset(-18);
        make.width.height.equalTo(sureBtn);
    }];
    
   
    
    [touchView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.insets(UIEdgeInsetsZero);
    }];

    

    [tap.rac_gestureSignal subscribeNext:^(__kindof UIGestureRecognizer * _Nullable x) {
        [weakSelf hidden];
    }];
    
    [[sureBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        if(weakSelf.selectIdx == -1){
            [QMUITips showError:@"请选择一个标签".rh_localized];
            return;
        }
        if (weakSelf.selectBlock) {
            weakSelf.selectBlock(weakSelf.selectIdx);
        }
        [weakSelf hidden];
    }];
    
    [[cancelBtn rac_signalForControlEvents:UIControlEventTouchUpInside]subscribeNext:^(__kindof UIControl * _Nullable x) {
        [weakSelf hidden];
    }];
}

- (void)hidden{
   
    [[RHWindowTipManage shareWindowTipManage] hidden];
}


#pragma mark - UIPickerViewDataSource,UIPickerViewDelegate
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 60;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArr.count;
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    RHTagModel *model = self.dataArr[indexPath.row];
    RHTagCell *cell = [tableView dequeueReusableCellWithIdentifier:@"RHTagCell"];
    NSArray *iconArr = @[@"network_tag_1",@"network_tag_2"];
    cell.icon.image = [UIImage imageNamed:iconArr[indexPath.row]];
    cell.titleLab.text = model.remark;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.tagImg.hidden = !model.isSelected;
    return cell;
}



-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    self.selectIdx = indexPath.row;
    for (RHTagModel *model in self.dataArr) {
        model.isSelected = NO;
    }
    RHTagModel *model = self.dataArr[indexPath.row];
    model.isSelected = !model.isSelected;
    
    [tableView reloadData];
    
    
}
@end
